package com.tsn.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsn.common.Result;
import com.tsn.entity.Personage;
import com.tsn.entity.Travel;
import com.tsn.service.TravelService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 游记 前端控制器
 * </p>
 *
 * @author tsn
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/tsn/travel")
public class TravelController {
    @Autowired
    private TravelService travelService;

    @GetMapping("/getTravels")
    public Result getTravels(){
        return Result.success(travelService.list());
    }

    @RequiresAuthentication
    @PostMapping("/addTravel")
    public Result addTravel(@RequestBody JSONObject article){
//        System.out.println(site);
        if(article==null) {
            return Result.failure("数据异常，请重试");
        }
//        Personage personage = new Personage();
        Travel travel = new Travel();
        travel.setTravelTitle(article.getString("title"));
        travel.setTravelAuthor(article.getString("editor"));
        travel.setContent(article.getString("content"));
        travelService.save(travel);
        return Result.success("添加成功",null);
    }
    @GetMapping("/getAttentionTravels")
    public  Result getAttentionTravels(@RequestParam("userId")Integer userId){
        return  travelService.listAttentionTravels(userId);
    }

    @GetMapping("/getTravelOne")
    public  Result getTravelById(@RequestParam("id")Integer id){
        return  Result.success(travelService.getById(id));
    }
    @GetMapping("/selectTravelByWord")
    public Result selectTravelByWord(@RequestParam("word") String word){
        QueryWrapper<Travel> travelQueryWrapper = new QueryWrapper<>();
        travelQueryWrapper.like("travel_title","%"+word+"%");
        List<Travel> list = travelService.list(travelQueryWrapper);
        return Result.success(list);

    }

}

